package com.liuzhu.chaoshikucungunli.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.liuzhu.chaoshikucungunli.model.Product;
import java.util.ArrayList;
import java.util.List;

public class ProductDao {
    private DBHelper dbHelper;

    public ProductDao(Context context) {
        dbHelper = new DBHelper(context);
    }

    // 添加商品
    public long addProduct(Product product) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_PRODUCT_NAME, product.getName());
        values.put(DBHelper.COLUMN_PRODUCT_CODE, product.getCode());
        values.put(DBHelper.COLUMN_PRODUCT_PRICE, product.getPrice());
        values.put(DBHelper.COLUMN_PRODUCT_QUANTITY, product.getQuantity());
        values.put(DBHelper.COLUMN_PRODUCT_UNIT, product.getUnit());
        values.put(DBHelper.COLUMN_PRODUCT_CATEGORY, product.getCategory());
        values.put(DBHelper.COLUMN_PRODUCT_DESC, product.getDescription());

        long id = db.insert(DBHelper.TABLE_PRODUCT, null, values);
        db.close();
        return id;
    }

    // 获取所有商品
    public List<Product> getAllProducts() {
        List<Product> products = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_PRODUCT, null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                Product product = new Product();
                product.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_ID)));
                product.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_NAME)));
                product.setCode(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_CODE)));
                product.setPrice(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_PRICE)));
                product.setQuantity(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_QUANTITY)));
                product.setUnit(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_UNIT)));
                product.setCategory(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_CATEGORY)));
                product.setDescription(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_DESC)));
                product.setCreateTime(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_CREATE_TIME)));
                product.setUpdateTime(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_UPDATE_TIME)));
                products.add(product);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return products;
    }

    // 更新商品信息
    public int updateProduct(Product product) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_PRODUCT_NAME, product.getName());
        values.put(DBHelper.COLUMN_PRODUCT_PRICE, product.getPrice());
        values.put(DBHelper.COLUMN_PRODUCT_QUANTITY, product.getQuantity());
        values.put(DBHelper.COLUMN_PRODUCT_UNIT, product.getUnit());
        values.put(DBHelper.COLUMN_PRODUCT_CATEGORY, product.getCategory());
        values.put(DBHelper.COLUMN_PRODUCT_DESC, product.getDescription());
        values.put(DBHelper.COLUMN_PRODUCT_UPDATE_TIME, "CURRENT_TIMESTAMP");

        String whereClause = DBHelper.COLUMN_PRODUCT_ID + " = ?";
        String[] whereArgs = {String.valueOf(product.getId())};

        int count = db.update(DBHelper.TABLE_PRODUCT, values, whereClause, whereArgs);
        db.close();
        return count;
    }

    // 删除商品
    public int deleteProduct(long productId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String whereClause = DBHelper.COLUMN_PRODUCT_ID + " = ?";
        String[] whereArgs = {String.valueOf(productId)};

        int count = db.delete(DBHelper.TABLE_PRODUCT, whereClause, whereArgs);
        db.close();
        return count;
    }

    // 根据商品编码查询商品
    public Product getProductByCode(String code) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Product product = null;

        String selection = DBHelper.COLUMN_PRODUCT_CODE + " = ?";
        String[] selectionArgs = {code};

        Cursor cursor = db.query(DBHelper.TABLE_PRODUCT, null, selection, selectionArgs, null, null, null);

        if (cursor.moveToFirst()) {
            product = new Product();
            product.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_ID)));
            product.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_NAME)));
            product.setCode(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_CODE)));
            product.setPrice(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_PRICE)));
            product.setQuantity(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_QUANTITY)));
            product.setUnit(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_UNIT)));
            product.setCategory(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_CATEGORY)));
            product.setDescription(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_DESC)));
            product.setCreateTime(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_CREATE_TIME)));
            product.setUpdateTime(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRODUCT_UPDATE_TIME)));
        }

        cursor.close();
        db.close();
        return product;
    }
} 